package com.d2rv.vrpn;

import processing.core.PVector;
import vrpn.TrackerRemote;

/**
 * Tracker listner for the VRPNClient
 * @author Darrieutort A., Dupouy A., Rossignol L., Van Poucke A.
 */
public class VRPNTrackerListener implements TrackerRemote.PositionChangeListener, TrackerRemote.VelocityChangeListener, TrackerRemote.AccelerationChangeListener {

    private PVector headposition;
    private PVector leftelbow;
    private PVector rightelbow;
    private PVector leftshoudler;
    private PVector rightshoudler;
    private PVector lefthip;
    private PVector righthip;

    /**
     * Default constructor. Shares the needed trackers positions with the VRPNClient.
     * @param headposition Head position
     * @param leftelbow Left elbow position
     * @param rightelbow Right elbow position
     * @param leftshoudler Left shoulder position
     * @param rightshoudler Right shoulder position
     * @param lefthip Left hip position
     * @param righthip Right hip position
     */
    public VRPNTrackerListener(PVector headposition, PVector leftelbow, PVector rightelbow, PVector leftshoudler, PVector rightshoudler, PVector lefthip, PVector righthip) {
        this.headposition = headposition;
        this.leftelbow = leftelbow;
        this.rightelbow = rightelbow;
        this.leftshoudler = leftshoudler;
        this.rightshoudler = rightshoudler;
        this.lefthip = lefthip;
        this.righthip = righthip;
    }

    /**
     * Updates the positions of the trackers
     * correspondance (FAAST) :
     * 0 : head
     * 5 : left shoulder (épaule)
     * 6 : left elbow (coude)
     * 11 : right shoulder (épaule)
     * 12 : right elbow (coude)
     * 16 : left hip (hanche)
     * 20 : right hip (hanche) 
     * @param u sensor to update
     * @param tracker 
     */
    public void trackerPositionUpdate(TrackerRemote.TrackerUpdate u, TrackerRemote tracker) {
        if (u.sensor == 0) {
            this.headposition.x = (float) u.pos[0];
            this.headposition.y = (float) u.pos[1];
            this.headposition.z = (float) u.pos[2];
        } 
        else if (u.sensor == 5) {
            this.leftshoudler.x = (float) u.pos[0];
            this.leftshoudler.y = (float) u.pos[1];
            this.leftshoudler.z = (float) u.pos[2];
        }
        else if (u.sensor == 6) {
            this.leftelbow.x = (float) u.pos[0];
            this.leftelbow.y = (float) u.pos[1];
            this.leftelbow.z = (float) u.pos[2];
        }
        else if (u.sensor == 11) {
            this.rightshoudler.x = (float) u.pos[0];
            this.rightshoudler.y = (float) u.pos[1];
            this.rightshoudler.z = (float) u.pos[2];
        }
        else if (u.sensor == 12) {
            this.rightelbow.x = (float) u.pos[0];
            this.rightelbow.y = (float) u.pos[1];
            this.rightelbow.z = (float) u.pos[2];
        }
        else if (u.sensor == 16) {
            this.lefthip.x = (float) u.pos[0];
            this.lefthip.y = (float) u.pos[1];
            this.lefthip.z = (float) u.pos[2];
        }
        else if (u.sensor == 20) {
            this.righthip.x = (float) u.pos[0];
            this.righthip.y = (float) u.pos[1];
            this.righthip.z = (float) u.pos[2];
        }
    }

    /**
     * No need to reimplement.
     * @param v
     * @param tracker
     */
    public void trackerVelocityUpdate(TrackerRemote.VelocityUpdate v, TrackerRemote tracker) {
    }

    /**
     * No need to reimplement.
     * @param a
     * @param tracker
     */
    public void trackerAccelerationUpdate(TrackerRemote.AccelerationUpdate a, TrackerRemote tracker) {
    }
}
